package com.zsd.android.ffmpegdemo.player;

import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class FFMPegPlayer implements SurfaceHolder.Callback {

    static {
        System.loadLibrary("native-lib");
    }

    private SurfaceHolder mSurfaceHolder;
    private String dataSource;
    private OnPrepareListener onPrepareListener;
    private OnProgressListener onProgressListener;
    private OnErrorListener onErrorListener;

    public void setSurfaceView(SurfaceView surfaceView) {
        if (null != mSurfaceHolder) {
            mSurfaceHolder.removeCallback(this);
        }
        mSurfaceHolder = surfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
    }

    public void prepare(String path) {
        this.dataSource = path;
        nativePrepare(dataSource);
    }

    public void start() {
        nativeStart();
    }

    /**
     * 给jni回调用
     * 表示播放器准备好了，可以播放了
     */
    public void onPrepare() {
        if (onPrepareListener != null) {
            onPrepareListener.onPrepare();
        }
    }

    /**
     * 给jni回调用，不断被调用
     * progress从jni返回回来
     */
    public void onProgress(int progress) {
        if (onProgressListener != null) {
            onProgressListener.onProgress(progress);
        }
    }

    /**
     * 给jni回调用
     * errorCode从jni返回回来
     */
    public void onError(int errorCode) {
        if (onErrorListener != null) {
            onErrorListener.onError(errorCode);
        }
    }

    public int getDuration() {
        return nativeGetDuration();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        nativeSetSurface(holder.getSurface());
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    public void setOnPrepareListener(OnPrepareListener onPrepareListener) {
        this.onPrepareListener = onPrepareListener;
    }

    public void setOnProgressListener(OnProgressListener onProgressListener) {
        this.onProgressListener = onProgressListener;
    }

    public void setOnErrorListener(OnErrorListener onErrorListener) {
        this.onErrorListener = onErrorListener;
    }

    public void seek(int progress) {
        new Thread(() -> {
            nativeSeek(progress);
        }).start();
    }

    public void release() {
        if (null != mSurfaceHolder) {
            mSurfaceHolder.removeCallback(this);
        }
        nativeRelease();
    }

    public void stop() {
        nativeStop();
    }

    public interface OnPrepareListener {
        void onPrepare();
    }

    public interface OnErrorListener {
        void onError(int error);
    }

    public interface OnProgressListener {
        void onProgress(int progress);
    }

    public native void nativePrepare(String dataSource);

    public native void nativeStart();

    public native void nativeSetSurface(Surface mSurface);

    public native int nativeGetDuration();

    public native void nativeSeek(int progress);

    public native void nativeStop();

    public native void nativeRelease();
}
